package com.wd.live.model.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * 作者：马海钊
 * 时间：2023/9/4 15:19
 * 功能：
 */
abstract class BaseActivity<VM : BaseViewModel, VDB : ViewBinding> : AppCompatActivity() {
    protected lateinit var binding:VDB
    protected lateinit var viewModel: VM
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=getViewBinding();
        setContentView(binding.root)
        initviewModel()
        initData()
    }
    private fun initviewModel() {
        val persistentClass =
            (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        viewModel = ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        )[persistentClass]
    }
    protected abstract fun initData();
    protected abstract fun getViewBinding():VDB
}
